package com.kstech.ks_gaz01.customView


import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import com.kstech.ks_gaz01.R
import com.kstech.ks_gaz01.data.DebugDataBean


/**
 * author: blankii
 * created on: 2025/11/13 10:21
 * description:
 */
class ZtProgressBarView : View {
    private val TAG = "DmCar"
    private var mOverTurn = false
    private var mPaintBlack: Paint = Paint()
    private var mPaintGreen: Paint = Paint()
    private var mViewWidth = 600f
    private var mViewHeight = 80f
    private var x_fenge1 = 0f
    private var x_fenge2 = 0f
    private var x_fenge3 = 0f

    constructor(mContext: Context) : this(mContext, null)
    constructor(mContext: Context, arrs: AttributeSet?) : this(mContext, arrs!!, 0)
    constructor(mContext: Context, attrs: AttributeSet, defStyleAttr: Int) : super(
        mContext, attrs, defStyleAttr
    ) {
        val a = context.theme.obtainStyledAttributes(
            attrs, R.styleable.ZtProgressBarView, 0, 0
        )
        try {
            mOverTurn = a.getBoolean(R.styleable.ZtProgressBarView_overturn, false)
        } finally {
            a.recycle()
        }
        init()
    }

    private fun init() {
        mPaintBlack.setColor(Color.BLACK)
        mPaintBlack.strokeWidth = 3f
        mPaintBlack.style = Paint.Style.STROKE
        mPaintBlack.isAntiAlias = true

        mPaintGreen.setColor(Color.GREEN)
        mPaintGreen.strokeWidth = 3f
        mPaintGreen.style = Paint.Style.FILL
        mPaintGreen.isAntiAlias = true

        val config = DebugDataBean.dataConfig_ZtProgress
        config(config.sMin, config.cMin, config.cMin1, config.cMin2, config.sMax)
//        updateValue(0)
    }

    fun config(sMin: Int, cMin: Int, cMin1: Int, cMin2: Int, sMax: Int) {
        x_fenge1 = ((cMin - sMin).toFloat() / (sMax - sMin)) * mViewWidth
        x_fenge2 = ((cMin1 - sMin).toFloat() / (sMax - sMin)) * mViewWidth
        x_fenge3 = ((cMin2 - sMin).toFloat() / (sMax - sMin)) * mViewWidth
//        "$x_fenge1,$x_fenge2，$x_fenge3".loge("blankii")
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(mViewWidth.toInt(), mViewHeight.toInt())
    }

    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas) {
//        if (mOverTurn) {
//            canvas.scale(-1f, 1f)
//        } else {
//
//        }
        canvas.drawRect(RectF(0f, 0f, mCurrentValue, mViewHeight), mPaintGreen)

        canvas.drawRect(RectF(0f, 0f, mViewWidth, mViewHeight), mPaintBlack)

        canvas.drawLine(x_fenge1, 0f, x_fenge1, mViewHeight, mPaintBlack)
        canvas.drawLine(x_fenge2, 0f, x_fenge2, mViewHeight, mPaintBlack)
        canvas.drawLine(x_fenge3, 0f, x_fenge3, mViewHeight, mPaintBlack)
        super.onDraw(canvas)
    }

    private var mCurrentValue = 0f
    fun updateValue(value: Int) {

        val config = DebugDataBean.dataConfig_ZtProgress
        if (value < config.sMin) {
            mCurrentValue = config.sMin.toFloat()
        } else if (value > config.sMax) {
            mCurrentValue = config.sMax.toFloat()
        }
        this.mCurrentValue =
            ((value - config.sMin).toFloat() / (config.sMax - config.sMin)) * mViewWidth

        postInvalidate()
    }
}